package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.FragmentNewCarEntity;
import com.shaoshaohuo.app.entity.NewPickEntity;
import com.squareup.picasso.Picasso;

import java.math.BigDecimal;
import java.util.List;

import static com.shaoshaohuo.app.R.id.sure_order_shop_share;

/**
 * Created by Administrator on 2017/6/10.
 */

public class SureOderShopAdapter extends BaseAdapter {
    private Context context;
    private List<FragmentNewCarEntity.DataBean> dataBeen;
    private List<NewPickEntity> data;
    private int child;
    private int share;
    private int yj;
    private float yjmoney;
    private float sharemoney;
    private float chidMoney;
    private String type = "1";
    private List<Integer> pickNum;

    public SureOderShopAdapter(Context context, List<Integer> pickNum, List<FragmentNewCarEntity.DataBean> dataBeen, String type, List<NewPickEntity> data, int childnum, int share, int yuanjianum) {
        this.context = context;
        this.type = type;
        if (this.type.equals("1")) {
            this.dataBeen = dataBeen;
        } else if (this.type.equals("2")) {
            this.data = data;
            this.child = childnum;
            this.share = share;
            this.yj = yuanjianum;
            this.pickNum = pickNum;
            Log.e("adapicknum", pickNum.size() + "");
        }
    }


    @Override
    public int getCount() {
        if (type.equals("1")) {
            return dataBeen != null ? dataBeen.size() : 0;
        } else if (type.equals("2")) {
            return pickNum != null ? pickNum.size() : 0;
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (type.equals("1")) {
            return dataBeen.get(position);
        } else if (type.equals("2")) {
            return pickNum.get(position);
        }
        return "";
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SureOderShopViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.sure_order_shop_item, parent, false);
            holder = new SureOderShopViewHolder();
            holder.sure_order_shop_picture = (ImageView) convertView.findViewById(R.id.sure_order_shop_picture);
            holder.sure_order_shop_name = (TextView) convertView.findViewById(R.id.sure_order_shop_name);
            holder.sure_order_shop_mbn_details = (TextView) convertView.findViewById(R.id.sure_order_shop_mbn_details);
            holder.sure_order_shop_details = (TextView) convertView.findViewById(R.id.sure_order_shop_details);
            holder.sure_order_shop_money = (TextView) convertView.findViewById(R.id.sure_order_shop_money);
            holder.sure_order_shop_share = (LinearLayout) convertView.findViewById(sure_order_shop_share);
            holder.sure_order_shop_shopmoney = (TextView) convertView.findViewById(R.id.sure_order_shop_shopmoney);
            holder.sure_order_shop_num = (TextView) convertView.findViewById(R.id.sure_order_shop_num);
            holder.sure_order_shop_hint = (LinearLayout) convertView.findViewById(R.id.sure_order_shop_hint);
            holder.sure_order_shop_address_linearLayout = (LinearLayout) convertView.findViewById(R.id.sure_order_shop_address_linearLayout);
            holder.sure_order_shop_address = (TextView) convertView.findViewById(R.id.sure_order_shop_address);
            holder.sure_order_shop_number = (TextView) convertView.findViewById(R.id.sure_order_shop_number);
            holder.sure_order_shop_lin = (LinearLayout) convertView.findViewById(R.id.sure_order_shop_lin);
            convertView.setTag(holder);
        } else {
            holder = (SureOderShopViewHolder) convertView.getTag();
        }
        if (type.equals("1")) {
            holder.sure_order_shop_share.setVisibility(View.GONE);
            holder.sure_order_shop_address_linearLayout.setVisibility(View.GONE);
            holder.sure_order_shop_hint.setVisibility(View.VISIBLE);
            holder.sure_order_shop_lin.setVisibility(View.VISIBLE);
            holder.sure_order_shop_money.setVisibility(View.VISIBLE);
            holder.sure_order_shop_details.setVisibility(View.VISIBLE);
            holder.sure_order_shop_mbn_details.setVisibility(View.VISIBLE);
            holder.sure_order_shop_details.setText("(一件起售  " + dataBeen.get(position).getDetails() + ")");
            int mbn_detailsType = dataBeen.get(position).getMbn_detailsType();
            //= =
            String num = dataBeen.get(position).getNum() == null ? "1" : dataBeen.get(position).getNum();
            holder.sure_order_shop_number.setText("×" + num);
            Log.e("mbn_detailsType", mbn_detailsType + "");
            if (mbn_detailsType == 1) {
                holder.sure_order_shop_mbn_details.setText("约" + dataBeen.get(position).getMbn_details() + "斤/件");
            } else if (mbn_detailsType == 2) {
                holder.sure_order_shop_mbn_details.setText("约" + dataBeen.get(position).getMbn_details() + "个/件");
            }
            holder.sure_order_shop_name.setText(dataBeen.get(position).getNames());
//            SpannableString spanMoney = new SpannableString();
//            AbsoluteSizeSpan span = new AbsoluteSizeSpan(20);
//            spanMoney.setSpan(span, 1, 3, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);

            BigDecimal b = new BigDecimal((Float.valueOf(dataBeen.get(position).getPrice()) * dataBeen.get(position).getMbn_details()));
            float f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
            holder.sure_order_shop_money.setText("￥" + f1);
            if (dataBeen.get(position).getImg() != null && dataBeen.get(position).getImg().length() > 0) {
                Picasso.with(context).load(dataBeen.get(position).getImg()).placeholder(R.mipmap.zhanzhanzhan).into(holder.sure_order_shop_picture);
            }
        } else if (type.equals("2")) {
            holder.sure_order_shop_lin.setVisibility(View.GONE);
            holder.sure_order_shop_share.setVisibility(View.VISIBLE);
            holder.sure_order_shop_address_linearLayout.setVisibility(View.VISIBLE);
            holder.sure_order_shop_hint.setVisibility(View.GONE);
            holder.sure_order_shop_money.setVisibility(View.GONE);
            holder.sure_order_shop_details.setVisibility(View.GONE);
            holder.sure_order_shop_mbn_details.setVisibility(View.GONE);
            holder.sure_order_shop_address.setText(data.get(0).getData().getAddress());
            if (pickNum.get(position) == 1) {
                holder.sure_order_shop_shopmoney.setText("￥" + data.get(0).getData().getChild());
                holder.sure_order_shop_num.setText("儿童票数量×" + child);
                chidMoney = child * Float.valueOf(data.get(0).getData().getChild());
            } else if (pickNum.get(position) == 2) {
                holder.sure_order_shop_shopmoney.setText("￥" + data.get(0).getData().getTeam());
                holder.sure_order_shop_num.setText("共享票数量×" + share);
                sharemoney = share * Float.valueOf(data.get(0).getData().getTeam());
            } else if (pickNum.get(position) == 3) {
                holder.sure_order_shop_shopmoney.setText("￥" + data.get(0).getData().getAdult());
                holder.sure_order_shop_num.setText("原价票数量×" + yj);
                yjmoney = yj * Float.valueOf(data.get(0).getData().getAdult());
            }
            holder.sure_order_shop_name.setText(data.get(0).getData().getName());
            float allMoney = yjmoney + chidMoney + sharemoney;
//            SpannableString spanMoney = new SpannableString("￥" + allMoney + "");
//            AbsoluteSizeSpan span = new AbsoluteSizeSpan(20);
//            spanMoney.setSpan(span, 1, 3, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
            holder.sure_order_shop_money.setText("￥" + allMoney);
            if (data.get(0).getData().getImg() != null && data.get(0).getData().getImg().length() > 0) {
                Picasso.with(context).load(data.get(0).getData().getImg()).into(holder.sure_order_shop_picture);
            }
        }
        return convertView;
    }


    class SureOderShopViewHolder {
        TextView sure_order_shop_number;
        ImageView sure_order_shop_picture;
        TextView sure_order_shop_name;
        TextView sure_order_shop_mbn_details;
        TextView sure_order_shop_details;
        LinearLayout sure_order_shop_lin;
        //        WhView sure_order_shop_add_reduce;
        TextView sure_order_shop_money;
        LinearLayout sure_order_shop_share;
        TextView sure_order_shop_shopmoney;
        TextView sure_order_shop_num;
        LinearLayout sure_order_shop_hint;
        LinearLayout sure_order_shop_address_linearLayout;
        TextView sure_order_shop_address;
    }


}
